package com.digitalchina.lxnsy.base;

import android.annotation.SuppressLint;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;

import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.LoginActivity;
import com.digitalchina.lxnsy.toolkit.AppInfo;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.tencent.smtt.sdk.WebView;

import java.util.Objects;

import butterknife.Bind;
import butterknife.ButterKnife;
import wendu.dsbridge.DWebView;


/**
 * ================================================
 * 项目名称：bigdata
 * 类描述：
 * 包名：com.digitalchina.bzas.activity
 * 创建人：郑皓
 * 创建时间：2018/12/3 13:59
 * 修改备注：
 * ================================================
 */

public abstract class BaseHtmlFragment extends BaseV4Fragment {


    @Bind(R.id.webView)
    protected DWebView webView;

    @Override
    public View setRootView() {
        return inflate(R.layout.activity_base_html);
    }

    protected abstract void loadHtml();

    @Override
    public void setLayout() {
    }

    @Override
    public void setHandler() {

    }

    @Override
    public void callBack(Object object) {

    }

    protected void callJsMethod(String method) {
        webView.callHandler(method, new String[]{""});
    }

    protected void callJsMethod(String method, String[] agrs) {
        webView.callHandler(method, agrs);
    }


    protected void loadUrl(String name) {
        webView.loadUrl("file:///android_asset/dist/" + name + ".html");
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initMember() {
        // set debug mode
        DWebView.setWebContentsDebuggingEnabled(false);
        webView.addJavascriptObject(new JsApiBase(), "base");


        com.tencent.smtt.sdk.WebSettings webSettings = webView.getSettings();
        String appCachePath = Objects.requireNonNull(getActivity()).getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        //用于js调用Android
        webSettings.setJavaScriptEnabled(true);
        //设置编码方式
        webSettings.setDefaultTextEncodingName("utf-8");
        //允许webview对文件的操作
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);

        webSettings.setPluginState(com.tencent.smtt.sdk.WebSettings.PluginState.ON);
        webView.setWebChromeClient(new com.tencent.smtt.sdk.WebChromeClient());

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setWebViewClient(webViewClient);

        loadHtml();
        isLogin();
    }
    private void isLogin(){
        callJsMethod("gotoLoginPage");
    }

    private com.tencent.smtt.sdk.WebViewClient webViewClient = new com.tencent.smtt.sdk.WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            webView.loadUrl(s);
            return true;

        }
    };

    @Override
    public void addListener() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public class JsApiBase {


        @JavascriptInterface
        public void logNative(Object msg) {
            LogUtils.e(msg);
        }

//        @JavascriptInterface
//        public String getUserAccId(Object msg) {
//            return UserXML.getUserAccId(getActivity());
//        }

        @JavascriptInterface
        public void closePage(Object msg) {
            getActivity().finish();
        }
    /*    //附件是图片的
        @JavascriptInterface
        public void openWebImgUrl(Object msg) {
            Log.d("a====",msg.toString());
            if (!StringUtil.isStrEmpty(msg.toString()))
                GotoUtil.gotoActivity(ImgShowActivity.class, "UrlVO", new UrlVO("详情", msg.toString()));
        }

        //附件预览
        @JavascriptInterface
        public void openWebUrl(Object msg) {
            GotoUtil.gotoActivity(activity, WebViewDetailActivity.class, "UrlVO", new UrlVO("附件预览", msg.toString()));
        }*/


        @JavascriptInterface
        public String getUserTypeJs(Object msg) {
            return UserXML.getUserType(getActivity());
        }

     /*   @JavascriptInterface
        public void openLogin(Object msg) {
            GotoUtil.gotoActivity(LoginActivity.class);
        }
*/
        @JavascriptInterface
        public String getVersionCode(Object msg) {
            return String.valueOf(AppInfo.getVersionCode(getActivity()));
        }

       /* @JavascriptInterface
        public void openLinkJs(Object msg) {
            GotoUtil.gotoActivityopenLink(msg.toString());
        }*/

    }


    /**
     * 无参跳转
     *
     * @param activity
     */
    protected void go(Class activity) {
        GotoUtil.gotoActivity(getActivity(), activity);
    }

}
